#include "stm32wig.h"

#include "ui_stm32wig.h"
#include <QTime>

Stm32Wig::Stm32Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Stm32Wig)
{
    ui->setupUi(this);

//    lcd1 = 25.6;
//    lcd2 = 54;

    m_db = QSqlDatabase::addDatabase("QSQLITE");
    m_db.setDatabaseName("test.db");
    if(!m_db.open())
    {
        qDebug() << m_db.lastError();
    }else
    {
        qDebug() << "打开成功";
    }

    m_query = new QSqlQuery(m_db);
    setwig = new SetWig;
    timer = new QTimer;
    datetime = new QTimer;
    count = new Count;

    connect(timer,&QTimer::timeout,this,&Stm32Wig::receivedataSlot);
    timer->start(3000);

    connect(datetime,&QTimer::timeout,this,&Stm32Wig::updateTime);
    datetime->start(1000);
}

Stm32Wig::~Stm32Wig()
{
    delete ui;
}

void Stm32Wig::dataUpSlot(const QByteArray &msg)
{
    qDebug() <<"enter dataUpdate";
    qDebug() << msg;
    QJsonDocument doc = QJsonDocument::fromJson(msg);
    if(!doc.isNull())
    {
        QJsonObject root = doc.object();
        int lcd_1 = root["temp"].toDouble();
        ui->lcd_temp->display(lcd_1);
        lcd1 = lcd_1;

        int lcd_2 = root["humi"].toInt();
        ui->lcd_humi->display(lcd_2);
        lcd2 = lcd_2;
    }
}

void Stm32Wig::receivedataSlot()
{
 //   qDebug()<< "temp:"<< lcd1 <<"humi:"<< lcd2;
    QString sql = "CREATE TABLE IF NOT EXISTS logs(temp DOUBLE,humi INT,time TEXT);";
    if(!m_query->exec(sql))
    {
        qDebug() << "CREATE TABLE:" << m_query->lastError();
    }
    QDateTime timeCur = QDateTime::currentDateTime();
    QString timeStr = timeCur.toString("yyyy-MM-dd hh:mm:ss");
//    qDebug() << timeStr;
    m_query->prepare("INSERT INTO logs(temp,humi,time) VALUES (:temp,:humi,:time);");
    m_query->bindValue(":temp",lcd1);
    m_query->bindValue(":humi",lcd2);
    m_query->bindValue(":time",timeStr);
    if(!m_query->exec())
    {
        qDebug() << "INSERT INTO:" << m_query->lastError();
    }
}

void Stm32Wig::updateTime()
{
    QDateTime currentDatime = QDateTime::currentDateTime();
    QString dateFormat = "yyyy年MM月dd日";
    QString timeFormat = "hh:mm:ss";
    ui->m_data->setText(currentDatime.toString(dateFormat));
    ui->m_time->setText(currentDatime.toString(timeFormat));
}


























































//    if(root.contains("data")&&root["data"].isArray())
//    {
//        QJsonArray dataArray = root["data"].toArray();
//        for(int i = 0;i < dataArray.size();i++)
//        {
//            QJsonValue dataValue = dataArray.at(i);
//            if(dataValue.isObject()){
//                QJsonObject dataObj = dataValue.toObject();
//                int key = dataObj["key"].toInt();
//                QString name = dataObj["name"].toString();
//                int data = dataObj["data"].toInt();
//                qDebug() << "Key:" << key << " Name:" << name << " Data:" << data;
//                if(key == 1)
//                {
//                    int lcd1 = dataObj["data"].toInt();
//                    ui->lcd_temp->display(lcd1);
//                }
//                if(key == 2)
//                {
//                    int lcd2 = dataObj["data"].toInt();
//                    ui->lcd_humi->display(lcd2);
//                }
//                if(key == 3)
//                {
//                    int lig_data = dataObj["data"].toInt();

//                    if(lig_data == 1)
//                    {
//                        ui->m_textlabel->setText("亮");
//                    }
//                    else
//                    {
//                        ui->m_textlabel->setText("灭");
//                    }
////                    emit lightUpSignal(light);
//                }
//            }
//        }
//    }
